<?php
namespace app\common\client;

use think\facade\Config;
use app\common\request\Curl;
use think\facade\Log;

class MessageClient {
    
    /**
     * 发送短信
     * @param int $number
     * @param String $content
     * @return boolean|mixed
     */
    public function sendMessage(String $cardCode,String $content,String $adminId,String $path){
        $data = [];
        $data['recNum'] = $cardCode;
        $data['templateParam'] = $content;
        $data['templateType'] = 2;
        $data['time'] = time();
        $data['adminId'] = $adminId;
        
        return $this->post($data, $path);
    }
    
    /**
     * 短信上行
     * @param int $page
     * @param int $length
     * @param String $search
     * @return boolean|mixed
     */
    public function smsUp(int $page,int $length,String $search){
        $data = [];
        $data['page'] = $page;
        $data['size'] = $length;
        $data['search'] = $search;
        
        return $this->post($data, 'show_smsup');
    }
    
   /**
    * 短信下行
    * @param int $start
    * @param int $length
    * @param String $search
    * @return boolean|mixed
    */
    public function smsDown(int $page,int $length,String $search){
        $data = [];
        $data['page'] = $page;
        $data['size'] = $length;
        $data['search'] = $search;
        
        return $this->post($data, 'show_send');
    }
    
    /**
     * 卡状态
     * @param String $simId
     * @return boolean|mixed
     */
    public function cardStatus(String $simId){
        $data = [];
        $data['iccid'] = $simId;
        
        return $this->post($data, 'card_status');
    }
    
    /**
     * 卡流量查询
     * @param String $simId
     * @return boolean|mixed
     */
    public function cardFlow(String $simId){
        $data = [];
        $data['iccid'] = $simId;
        
        return $this->post($data, 'card_flow_info');
    }
    
    /**
     * 套餐列表查询
     * @param String $simId
     * @return boolean|mixed
     */
    public function packageInfo(String $simId){
        $data = [];
        $data['iccid'] = $simId;
        
        return $this->post($data, 'iot_card_offer_dtl');
    }
    
    public function post($data,$path){
        $headers = [];
        $headers[] = 'Sign:'.'C1A21A67C70CBC66F9FC3BF5B1F64D43';
        $url = Config::get('message.url').$path;
        
        $curl = new Curl; 
        $curl->set('CURLOPT_HTTPHEADER',$headers)->post(http_build_query($data))->url($url);
        
        if ($curl->error()) {
            
            Log::record('[ 请求错误 ] '.$curl->message(),'error');
            return false;
        } else {
            
            $info = $curl->info();
            
            if($info['http_code'] == 200){
                $arr = json_decode($curl->data(),true);
                if($arr !== null){
                    return $arr;
                }else{
                    Log::record('[ 解码失败 ] '.$curl->data(),'error');
                    return false;
                }
            }else{
                Log::record('[ 请求目标 ] '.$curl->data() .$info['http_code'],'error');
                return false;
            }
        }
    }
}